package com.runa.rccprotocol.analysis;

import com.runa.monitor.comm.dto.newComm.PanelVO;
import com.runa.persistence.util.LogUtil;
import com.runa.protocol.analysis.MetaBufferAnalysis;
import com.runa.protocol.analysis.ShellBufferAnalysis;
import com.runa.protocol.dto.DtuReportAck;
import com.runa.protocol.util.ByteUtil;
import com.runa.protocol.util.UtilTime;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;

import java.util.Collections;
import java.util.Objects;

public class SmartHeatAnalysis extends ShellBufferAnalysis<byte[]> {
    public SmartHeatAnalysis() {
        super(null, 0);
    }

    @Override
    public int fill(byte[] data, int length, String netEquNo) {
        LogUtil.getLogAndInfo("message", "SH_TMP", new String(data));
        String[] msg = new String(data).split(";");
        String[] equData = msg[3].split(",");
        if (Objects.equals(msg[0], "TX") && Objects.equals(msg[2], "DATA")) {
            this.netEquNo = equData[1];
            PanelVO panelVO = new PanelVO();
            panelVO.setSerial(equData[1]);
            panelVO.setReadTime(UtilTime.StringToDate(equData[3], "yyMMddHHmmss"));
            panelVO.setTemperature(Float.parseFloat(equData[4]));
            panelVO.setHumidity(Float.parseFloat(equData[7]));
            panelVO.setSignal(equData[9]);
            panelVO.setBatteryVol(Float.valueOf(equData[12]));
            panelVO.setStatus(equData[23]);
            panelVO.setHardVersion(equData[24]);
            panelVO.setSoftVersion(equData[25]);
            RunaConcenResponseData responseData = new RunaConcenResponseData();
            responseData.setAddress(equData[1]);
            responseData.addRepData(Collections.singletonList(panelVO));
            responseData.setUpward(true);
            responseData.setAck(new DtuReportAck() {
                @Override
                public byte[] getData() {
                    return "BYE".getBytes();
                }

                @Override
                public String getAddr() {
                    return equData[1];
                }
            });
            this.interData = responseData;
        }
        return 0;
    }


    public static void main(String[] args) {
        SmartHeatAnalysis analysis = new SmartHeatAnalysis();
        byte[] bytes = ByteUtil.hexStringToBytes("54 58 3B 56 32 34 3B 44 41 54 41 3B 54 34 30 31 2C 38 36 30 31 33 35 30 36 35 32 32 39 37 34 35 2C 38 39 38 36 30 34 46 32 31 30 32 33 44 31 33 30 37 35 39 39 2C 32 34 30 39 32 39 31 32 33 31 30 32 2C 31 38 2E 38 2C 30 2E 30 2C 32 30 2E 30 2C 30 2C 30 2E 30 2C 32 39 2C 33 32 2C 30 2C 33 2E 36 2C 33 30 2C 33 30 2C 31 30 30 31 2C 39 33 30 2C 31 2E 35 2C 30 2C 33 32 2C 31 32 2C 32 2C 31 2C 30 2C 33 2E 30 2E 30 2C 31 2E 32 2E 34 3B 32 32 37 3B");
        analysis.fill(bytes, bytes.length, null);
        System.out.println(new String(bytes));
    }
}

